<template>
	<view class="content">
		<view class="baby">
			<view class="babyItem" v-for="(item ,index) in babyList" @click="redact(item)">
				<image :src="item.pic" mode=""></image>
				<view class="name">
					{{item.user}}
				</view>
				<view class="esc" @click.stop="esc(item)">
					<u-icon name="close-circle" color="red" size="40"></u-icon>
				</view>
			</view>
		</view>

		<u-popup v-model="show" mode="top">
			<view class="addBaby">
				<view class="" style="width: 200upx; margin: 0 auto; ">
					<u-upload :action="action" :file-list="picImg" max-count="1" upload-text="上传宝贝头像" @on-change="upImg"
						@on-remove="escImg" ref="upload"></u-upload>
				</view>
				<view class="inp">
					<input type="text" value="" placeholder="请输入宝贝姓名" v-model="user" />
				</view>
				<view class="submit" @click="submit">
					确定
				</view>
			</view>


		</u-popup>

		<view class="btn" @click="show=true">
			添加宝贝
		</view>
		<u-toast ref="uToast" />
	</view>

</template>

<script>
	export default {
		data() {
			return {
				action: 'https://ilexue.top/home/upload/upload',
				show: false,
				picImg: [],
				pic: '',
				user: '',
				baby_id: '',
				babyList: [],
			}
		},
		onLoad() {
			this.newData()
		},
		methods: {
			newData() {
				this.$myRuquest('home/user/baby_list').then(res => {
					if (res.code == 400) {
						this.babyList = res.data
					}
				})
			},
			upImg(e) {
				let res = JSON.parse(e.data)
				if (res.code == 400) {
					this.$refs.uToast.show({
						title: res.msg,
						type: 'success'
					})
					this.pic = res.data
				} else {
					this.$refs.uToast.show({
						title: res.msg,
						type: 'error'
					})
				}
			},
			escImg(index) {
				this.pic = ''
			},
			submit() {
				if (!this.pic) return this.$refs.uToast.show({
					title: '请上传宝贝头像',
					type: 'warning '
				});
				if (!this.user) return this.$refs.uToast.show({
					title: '请输入宝贝姓名',
					type: 'warning '
				});
				let data = {
					pic: this.pic,
					user: this.user
				}
				if (this.baby_id) data.baby_id = this.baby_id;
				this.$myRuquest('home/user/baby_save', data, 'POST').then(res => {
					if (res.code == 400) {
						this.$refs.uToast.show({
							title: res.msg,
							type: 'success'
						})
						this.pic = ''
						this.user = ''
						this.baby_id = ''
						this.picImg=[]
						this.$refs.upload.lists = []
						this.show = false
						this.newData()
					}
				})
			},
			redact(item) {
				this.$refs.upload.lists = []
				this.baby_id = item.id
				this.$refs.upload.lists = [{
					url: item.pic
				}]
				this.pic = this.escUrl(item.pic)

				this.user = item.user
				this.show = true
			},
			escUrl(str) {
				if (!str) return str;
				return str.replace(/https:\/\/ilexue.top/, "")
			},
			esc(item) {
				let that = this
				uni.showModal({
					title: '提示',
					content: `你确定要删除 ${item.user} 吗？`,
					success: function(res) {
						if (res.confirm) {
							that.$myRuquest('home/user/baby_del', {
								baby_id: item.id
							}, 'DELETE').then(res => {
								if (res.code == 400) {
									that.$refs.uToast.show({
										title: res.msg,
										type: 'success'
									})
									that.newData()
								}else{
									that.$refs.uToast.show({
										title: res.msg,
										type: 'warning '
									})
								}
							})
						} else if (res.cancel) {
							that.$refs.uToast.show({
								title: '已取消',
								type: 'success'
							})
						}
					}
				});
			}
		}
	}
</script>

<style lang="less" scoped>
	.baby {
		display: flex;
		flex-wrap: wrap;
		width: calc(100% - 40upx);
		margin: 20upx;

		.babyItem {
			width: calc(29% -20upx);
			margin: 20upx;
			padding: 10upx;
			position: relative;
			position: relative;


			background-color: rgba(0, 0, 0, .1);
			box-shadow: 0 0 5upx rgba(0, 0, 0, .2);
			border-radius: 12upx;

			image {
				width: 160upx;
				height: 160upx;
				margin: 0 10upx;
				border-radius: 8upx;
			}

			.name {
				text-align: center;
			}

			.esc {
				position: absolute;
				top: -20upx;
				right: -20upx;
			}
		}
	}




	.addBaby {
		padding: 20upx;

		.inp {
			width: 600upx;
			background-color: rgb(247,247,247);
			padding: 20upx;
			margin: 0 auto;
			border-radius: 12upx;
		}
	}

	.submit {
		width: 220upx;
		height: 70upx;
		line-height: 70upx;
		text-align: center;
		margin: 0upx auto;
		margin-top: 40upx;
		border-radius: 12upx;
		background: linear-gradient(to right, rgb(254, 189, 140), rgb(254, 28, 77));
	}

	.btn {
		width: 500upx;
		height: 80upx;
		line-height: 80upx;
		text-align: center;
		margin: 100upx auto;
		border-radius: 12upx;
		color: #fff;
		background: linear-gradient(to right, rgb(254, 189, 140), rgb(254, 28, 77));
	}
</style>
